package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author: lei
 * @Date 2021/11/25 14:59
 **/
@RestController
//@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 供应商信息分页展示
     * 请求URL：http://localhost:8080/supplier/list
     * 请求参数：Integer page（当前页数）, Integer rows（每页显示的记录数）, String supplierName
     * 请求方式：POST
     * 返回值类型： JSON
     * 返回值：Map<String,Object>
     * @param request
     * @return
     */
    @RequestMapping("/supplier/list")
    public Map list(HttpServletRequest request){
        String page = request.getParameter("page");
        String rows = request.getParameter("rows");
        String supplierName = request.getParameter("supplierName");

        Map<String,Object> map= supplierService.list(page,rows,supplierName);
        return map;

    }

    /**
     * 请求URL：http://localhost:8080/supplier/save?supplierId=1
     * 请求参数：Supplier supplier
     * 请求方式：POST
     * 返回值类型： JSON
     * 返回值：ServiceVO
     * Response Example Value：
     */
    @PostMapping("/supplier/save")
//    public ServiceVO save(@RequestBody Supplier supplier, @RequestParam(value = "supplierId",required = false)Integer supplierId){
    public ServiceVO save(HttpServletRequest request,@RequestParam(value = "supplierId",required = false)Integer supplierId){
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Supplier supplier = new Supplier();
            BeanUtils.populate(supplier,parameterMap);
            supplier.setSupplierId(supplierId);
            supplierService.saveOrUpdate(supplier);


            ServiceVO serviceVO = new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
//        Gson gson = new Gson();
            return serviceVO;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 删除供应商，支持批量删除
     * 请求URL：http://localhost:8080/supplier/delete
     * 请求参数：String  ids
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     * Response Example Value：
     * {
     *     "code": 100,
     *     "msg": "请求成功",
     *     "info": null
     * }
     */

    @PostMapping("supplier/delete")
    public ServiceVO delete(HttpServletRequest request){
        String ids = request.getParameter("ids");
        supplierService.delete(ids);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }
}
